package com.lekang.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lekang.common.annotation.Log;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.common.enums.BusinessType;
import com.lekang.system.domain.Elderly;
import com.lekang.system.service.IElderlyService;
import com.lekang.common.utils.poi.ExcelUtil;
import com.lekang.common.core.page.TableDataInfo;

/**
 * 老人Controller
 * 
 * @author ruoyi
 * @date 2025-10-14
 */
@RestController
@RequestMapping("/system/elderly")
public class ElderlyController extends BaseController
{
    @Autowired
    private IElderlyService elderlyService;

    /**
     * 查询老人列表
     */
//    @PreAuthorize("@ss.hasPermi('system:elderly:list')")
    @GetMapping("/list")
    public TableDataInfo list(Elderly elderly)
    {
//        startPage();
        List<Elderly> list = elderlyService.selectElderlyList(elderly);
        return getDataTable(list);
    }

    /**
     * 导出老人列表
     */
//    @PreAuthorize("@ss.hasPermi('system:elderly:export')")
//    @Log(title = "老人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Elderly elderly)
    {
        List<Elderly> list = elderlyService.selectElderlyList(elderly);
        ExcelUtil<Elderly> util = new ExcelUtil<Elderly>(Elderly.class);
        util.exportExcel(response, list, "老人数据");
    }

    /**
     * 获取老人详细信息（含护理等级、床位、养老顾问、护理员等）
     */
//    @PreAuthorize("@ss.hasPermi('system:elderly:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(elderlyService.selectElderlyDetailById(id));
    }

    /**
     * 新增老人
     */
//    @PreAuthorize("@ss.hasPermi('system:elderly:add')")
//    @Log(title = "老人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Elderly elderly)
    {
        return toAjax(elderlyService.insertElderly(elderly));
    }

    /**
     * 修改老人
     */
//    @PreAuthorize("@ss.hasPermi('system:elderly:edit')")
//    @Log(title = "老人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Elderly elderly)
    {
        return toAjax(elderlyService.updateElderly(elderly));
    }

    /**
     * 删除老人
     */
//    @PreAuthorize("@ss.hasPermi('system:elderly:remove')")
//    @Log(title = "老人", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(elderlyService.deleteElderlyByIds(ids));
    }



    /**
     * 根据身份证号查询老人的id和name
     * @param idCard 身份证号
     * @return 包含id和name的Elderly对象（仅id和name字段有效，无数据则返回null）
     */
    @GetMapping("/id-name-by-idcard")
    public AjaxResult getElderlyIdAndNameByIdCard(String idCard) {
        Elderly elderly = elderlyService.getElderlyIdAndNameByIdCard(idCard);
        if (elderly == null) {
            return AjaxResult.error("未找到该身份证对应的老人"); // 明确返回错误信息
        }
        return AjaxResult.success(elderly); // 用统一格式包装成功结果
    }


}
